package org.market.domain.strategy.service.rule.chain.factory;

import lombok.*;
import org.market.domain.strategy.model.entity.StrategyEntity;
import org.market.domain.strategy.repository.IStrategyRepository;
import org.market.domain.strategy.service.rule.chain.ILogicChain;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/24 上午9:21
 * @Version: 1.0
 */
@Service
public class DefaultChainFactory {
    protected final Map<String, ILogicChain> logicChainMap;
    protected final IStrategyRepository strategyRepository;

    public DefaultChainFactory(Map<String, ILogicChain> logicChainMap, IStrategyRepository strategyRepository) {
        this.logicChainMap = logicChainMap;
        this.strategyRepository = strategyRepository;
    }

    public ILogicChain buildLogicChain(Long strategyId) {
        StrategyEntity strategy = strategyRepository.queryStrategyEntityList(strategyId);
        String[] ruleModels = strategy.RuleModels();

        if (ruleModels == null || ruleModels.length == 0) return logicChainMap.get("default");

        ILogicChain loginChain = logicChainMap.get(ruleModels[0]);
        ILogicChain current = loginChain;
        for (int i = 1; i < ruleModels.length; i++) {
            ILogicChain next = logicChainMap.get(ruleModels[i]);
            current = current.appendNext(next);
        }
        current.appendNext(logicChainMap.get("default"));
        return loginChain;
    }


    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StrategyAwardVO {
        private Integer awardId;
        private String awardRuleVale;
    }

    @Getter
    @AllArgsConstructor
    public enum logicMode{
        RULE_DEFAULT("default","默认抽奖"),
        RULE_BLACKLIST("rule_blacklist","黑名单"),
        RULE_WEIGHT("rule_weight","权重抽奖模式");

        private final String code;
        private final String info;
    }
}
